/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.screens;

import com.cmp.arena.Arena;
import com.cmp.arena.controls.TimeControl;
import static com.cmp.core.constants.PlayerStateUIDConstants.TEAM_UID;
import static com.cmp.core.constants.UIDConstants.*;
import com.cmp.player.controls.ScoreControl;
import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.Screen;



/**
 * Bundles nodes for the arena and UI stuff.
 * @author krummer
 */
public class GameScreen extends ScreenAppController {

    //Nifty GUI stuff
    private Element scoreLabel1;
    private Element scoreLabel2;
    private Element timeLabel;
    private Element msgLabel;
    
    //GameApp stuff
    private Arena arena;
    
    /* GUI CALL FUNCTIONS */
    public void updateScore() {
        
        Boolean hasChanged = arena.getMapNode().getUserData(SCORE_CHANGE_UID);
        
        if((hasChanged != null) && hasChanged.booleanValue()) {
            int scoreA = 0;
            int scoreB = 0;
            
            for(Node player : arena.getPlayersList()) {
                boolean teamA = player.getUserData(TEAM_UID);
                Integer score = player.getControl(ScoreControl.class).getScore();
                               
                if(teamA) {
                    scoreA += score.intValue(); 
                } else {
                    scoreB += score.intValue();
                }
            }
            
            this.scoreLabel1.getRenderer(TextRenderer.class)
                    .setText(""+scoreA);
            this.scoreLabel2.getRenderer(TextRenderer.class)
                    .setText(""+scoreB);
            
            //set hasChanged flag, so that it can register new goals.
            arena.getMapNode().setUserData(SCORE_CHANGE_UID, false);
        }
    }
    
    public String getMatchTime() {
        String matchTimeString = "";
        TimeControl timeControl = arena.getMapNode().getControl(TimeControl.class);
        
        if(timeControl != null) {
            matchTimeString = "" +(int) timeControl.getMatchTime();
        }
        
        return matchTimeString;
    }
    
    public Element getMsgLabel() {
        return this.msgLabel;
    }
    
    /* app state overrides */ 
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
       super.initialize(stateManager, app);
       
       //load game
       this.arena = app.getStateManager().getState(Arena.class);
       this.getLocalRootNode().attachChild(arena.getMapNode());    
    }
    
    @Override
    public void update(float tpf) {
        
        //if one elemnt is eneabled, we can assume that all are.
        if(this.isEnabled() && (this.scoreLabel1 != null)) {
            this.updateScore();
           
            //update time
            this.timeLabel.getRenderer(TextRenderer.class)
                    .setText(this.getMatchTime());
        }
        
    }

    /* screen controller overrides */
    @Override
    public void bind(Nifty nifty, Screen screen) {
        super.bind(nifty, screen);
        
        //find elements we want to change.
        this.scoreLabel1 = screen.findElementByName("scoreLabel1");
        this.scoreLabel2 = screen.findElementByName("scoreLabel2");
        this.timeLabel = screen.findElementByName("matchTimeLabel");
        this.msgLabel = screen.findElementByName("announceMsg");
    }
    
    @Override
    public void onStartScreen() {
        super.onStartScreen();
        this.arena.loadMap();
    }

}
